﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using OpenTK.Graphics;

namespace _2D_Game
{
    class Level
    {
        public Level()
        {
            _terrainData = new Dictionary<Point, Square>();
        }

        public const int TILE_SIZE = 25;

        private Dictionary<Point, Square> _terrainData;

        public void Render()
        {
            GL.PushMatrix();
            GL.Translate(200, 200, 0);

            foreach (var square in _terrainData)
            {
                square.Value.Render();
            }

            GL.PopMatrix();
        }

        public static Level ParseXml(string filename)
        {
            var level = XElement.Load(filename);
            if (level == null)
                throw new XmlException("It returned null");

            var retLev = new Level();

            foreach (var squ in level.Elements())
            {
                Console.WriteLine(String.Format("({0}, {1})", (int)squ.Attribute("x"), (int)squ.Attribute("y")));
                Point p = new Point((int) squ.Attribute("x"), (int) squ.Attribute("y"));
                Square square = new Square((int) squ.Attribute("x")*TILE_SIZE, (int) squ.Attribute("y")*TILE_SIZE,
                                        (int) squ.Attribute("width")*TILE_SIZE, (int) squ.Attribute("height")*TILE_SIZE);
                square.SetTexture(TextureManager.LoadTexture("..\\..\\" + squ.Element("Graphic").Attribute("location").Value));
                retLev._terrainData.Add(p, square);
            }

            return retLev;
        }
    }
}
